﻿package  {
	
	//import Cursor.*;
	//import Fiducial.*;
	
	import flash.desktop.NativeApplication;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.*;
	import flash.geom.Transform;
	
	import org.tuio.*;
	import org.tuio.TuioFiducialEvent;
	import org.tuio.TuioTouchEvent;
	import org.tuio.connectors.*;
	import org.tuio.gestures.*;
	
	public class Main extends MovieClip {
		var tc:TuioClient;
		var conn:UDPConnector;
		
		var cCursor:Cursor;
		var cFiducial:Fiducial;
		var dObject:DragObject;
		
		var myCard:Card;
		
		public function Main() {
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.displayState = StageDisplayState.FULL_SCREEN;
			
			conn = new UDPConnector("127.0.0.1",3333);
			tc = new TuioClient(conn);
			var tm:TuioManager = TuioManager.init(stage);
			var gm:GestureManager = GestureManager.init(stage);	
			tc.addListener(tm);
			
			GestureManager.addGesture(new DragGesture());
			GestureManager.addGesture(new ZoomGesture(TwoFingerMoveGesture.TRIGGER_MODE_TOUCH));
			GestureManager.addGesture(new RotateGesture(TwoFingerMoveGesture.TRIGGER_MODE_TOUCH));
			
			dObject = new DragObject();
			dObject.x = stage.stageWidth/2;
			dObject.y = stage.stageHeight/2;
			stage.addChild(dObject);
			
			myCard = new Card();
			myCard.x =100;
			myCard.y = 100;
			stage.addChild(myCard);
			
			myCard.addEventListener(TransformGestureEvent.GESTURE_PAN, handleDrag);
			myCard.addEventListener(TransformGestureEvent.GESTURE_ZOOM, handleZoom);
			myCard.addEventListener(TransformGestureEvent.GESTURE_ROTATE, handleRotate);
			
			dObject.addEventListener(TransformGestureEvent.GESTURE_PAN, handleDrag);
			dObject.addEventListener(TransformGestureEvent.GESTURE_ZOOM, handleZoom);
			dObject.addEventListener(TransformGestureEvent.GESTURE_ROTATE, handleRotate);
			
			tm.addEventListener(TuioEvent.ADD_CURSOR, addHandler);
			tm.addEventListener(TuioEvent.UPDATE_CURSOR, updateHandler);
			tm.addEventListener(TuioEvent.REMOVE_CURSOR, removeHandler);
			stage.addEventListener(TuioFiducialEvent.ADD, addFiducial);
			stage.addEventListener(TuioFiducialEvent.MOVE, updateFiducial);
			stage.addEventListener(TuioFiducialEvent.REMOVED, removeFiducial);
			
		}
		
		public function handleDrag(e:TransformGestureEvent){
			e.target.x += e.offsetX;
			e.target.y += e.offsetY;
		}
		
		public function handleZoom(e:TransformGestureEvent){
			e.target.scaleX *= e.scaleX;
			e.target.scaleY *= e.scaleY;
		}
		
		public function handleRotate(e:TransformGestureEvent){
			e.target.rotation += e.rotation;
		}
		
		public function addFiducial(e:TuioFiducialEvent):void{
			var nFiducial:Fiducial = new Fiducial("Fid" + e.tuioObject.classID.toString(), stage, e.tuioObject.x * stage.stageWidth, e.tuioObject.y * stage.stageHeight, e.tuioObject.classID.toString());
		}
		
		public function updateFiducial(e:TuioFiducialEvent):void {
			cFiducial = stage.getChildByName("Fid" + e.tuioObject.classID.toString()) as Fiducial;
			cFiducial.rotation = e.rotation;
			cFiducial.x = e.tuioObject.x * stage.stageWidth;
			cFiducial.y = e.tuioObject.y * stage.stageHeight;
		}
		
		public function removeFiducial(e:TuioFiducialEvent):void{
			cFiducial = stage.getChildByName("Fid" + e.tuioObject.classID.toString()) as Fiducial;
			stage.removeChild(cFiducial);
		}
		
		public function addHandler(e:TuioEvent):void{
			var nCursor:Cursor = new Cursor(e.tuioContainer.sessionID.toString(), stage, e.tuioContainer.x*stage.stageWidth, e.tuioContainer.y * stage.stageHeight); 
		}
		
		public function updateHandler(e:TuioEvent):void {
			cCursor = stage.getChildByName(e.tuioContainer.sessionID.toString()) as Cursor;
			cCursor.x = e.tuioContainer.x * stage.stageWidth;
			cCursor.y = e.tuioContainer.y * stage.stageHeight;
			
			trace("----- \nType: " + e.tuioContainer.type + "\nx: " + e.tuioContainer.x + "\ny: " + e.tuioContainer.y + "\nX: " + e.tuioContainer.X + "\nY: " + e.tuioContainer.Y + "\nm: " + e.tuioContainer.m);
		}

		public function removeHandler(e:TuioEvent):void {
  			cCursor = stage.getChildByName(e.tuioContainer.sessionID.toString()) as Cursor;
 			stage.removeChild(cCursor);
		}
	}
}